#include "Geometry.h"
#include "Renderer.h"

Geometry::Geometry( unsigned int num_vertices, Vector3* vertex, Vector3* normal,
				   Color* color, Vector2* texture )
{
	m_num_vertices = num_vertices;
	m_vertex  = vertex;
	m_normal  = normal;
	m_color   = color;
	m_texture = texture;
}

Geometry::Geometry()
{
	m_num_vertices = 0;
	m_vertex  = 0;
	m_normal  = 0;
	m_color   = 0;
	m_texture = 0;
}

Geometry::~Geometry()
{
	delete[] m_vertex;
	delete[] m_normal;
	delete[] m_color;
	delete[] m_texture;
}

void Geometry::reconstruct( unsigned int num_vertices )
{
	m_num_vertices = num_vertices;

	delete[] m_vertex;
	delete[] m_normal;
	delete[] m_color;
	delete[] m_texture;

	if ( num_vertices > 0 )
	{
		m_vertex = new Vector3[m_num_vertices];

		if ( m_normal )
			m_normal = new Vector3[m_num_vertices];
		else
			m_normal = 0;

		if ( m_color )
			m_color = new Color[m_num_vertices];
		else
			m_color = 0;

		if ( m_texture )
			m_texture = new Texture[m_num_vertices];
		else
			m_texture = 0;
	}
	else
	{
		m_vertex  = 0;
		m_normal  = 0;
		m_color   = 0;
		m_texture = 0;
	}
}

void Geometry::reconstruct( unsigned int num_vertices, Vector3* vertex,
						   Vector3* normal, Color* color, Vector2* texture )
{
	m_num_vertices = num_vertices;

	if ( m_vertex != vertex )
	{
		delete[] m_vertex;
		m_vertex = vertex;
	}

	if ( m_normal != normal )
	{
		delete[] m_normal;
		m_normal = normal;
	}

	if ( m_color != color )
	{
		delete[] m_color;
		m_color = color;
	}

	if ( m_texture != texture )
	{
		delete[] m_texture;
		m_texture = texture;
	}
}

void Geometry::update_model_bound()
{
	m_bound.compute_from_data( m_num_vertices, m_vertex );
}

void Geometry::update_world_bound()
{
	m_world_bound = m_bound.transform_by( m_world_rotate, m_world_translate,
		m_world_scale );
}

void Geometry::update_model_normals()
{
}

void Geometry::update_render_state( RenderState* state[] )
{
	for ( unsigned int i = 0; i < RenderState::RS_MAX_STATE; i++ )
		m_state[i] = state[i]->copy();
}

void Geometry::draw( Renderer& renderer )
{
	renderer.set_state( m_state );
}